{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "material-latvia",
   "metadata": {},
   "source": [
    "# Italian Code For Identification Of Drugs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "engaging-institution",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "attached-alloy",
   "metadata": {},
   "source": [
    "The function `clean_it_aic()` cleans a column containing Italian code for identification of drug (AIC) strings, and standardizes them in a given format. The function `validate_it_aic()` validates either a single AIC strings, a column of AIC strings or a DataFrame of AIC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unexpected-theorem",
   "metadata": {},
   "source": [
    "AIC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"000307052\"\n",
    "* `standard`: AIC strings with proper whitespace in the proper places. Note that in the case of AIC, the compact format is the same as the standard one.\n",
    "* `base10`: convert a BASE32 representation to a BASE10 one, like \"000307052\".\n",
    "* `base32`: convert a BASE10 representation to a BASE32 one, like \"009CVD\". Note 'compact' may contain both BASE10 and BASE32 represatation.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_it_aic()` and `validate_it_aic()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dirty-federal",
   "metadata": {},
   "source": [
    "### An example dataset containing AIC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imperial-complex",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"aic\": [\n",
    "            '000307052',\n",
    "            '999999',\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008',\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deadly-script",
   "metadata": {},
   "source": [
    "## 1. Default `clean_it_aic`\n",
    "\n",
    "By default, `clean_it_aic` will clean aic strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "stuck-health",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_it_aic\n",
    "clean_it_aic(df, column = \"aic\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "functional-coating",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "forced-botswana",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "pressing-miracle",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "suited-electron",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, column = \"aic\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "romantic-difference",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "constant-vinyl",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, column = \"aic\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complimentary-chair",
   "metadata": {},
   "source": [
    "### `base10`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "concerned-moses",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, column = \"aic\", output_format=\"base10\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "threatened-pantyhose",
   "metadata": {},
   "source": [
    "### `base32`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sharp-ferry",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, column = \"aic\", output_format=\"base32\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "electronic-eight",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned AIC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "radical-premises",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, column=\"aic\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "experienced-combine",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unexpected-lying",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "inner-tokyo",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, \"aic\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adjustable-collar",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "amino-armenia",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_it_aic(df, \"aic\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acoustic-princeton",
   "metadata": {},
   "source": [
    "## 4. `validate_it_aic()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "commercial-dubai",
   "metadata": {},
   "source": [
    "`validate_it_aic()` returns `True` when the input is a valid AIC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_it_aic()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_it_aic()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_it_aic()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "statistical-lightweight",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_it_aic\n",
    "print(validate_it_aic('000307052'))\n",
    "print(validate_it_aic('999999'))\n",
    "print(validate_it_aic('7542011030'))\n",
    "print(validate_it_aic('7552A10004'))\n",
    "print(validate_it_aic('8019010008'))\n",
    "print(validate_it_aic(\"hello\"))\n",
    "print(validate_it_aic(np.nan))\n",
    "print(validate_it_aic(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "compliant-sapphire",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "simple-football",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_it_aic(df[\"aic\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "progressive-coral",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dying-background",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_it_aic(df, column=\"aic\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hidden-scotland",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "binary-twelve",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_it_aic(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "union-garage",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
